﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace l1cs.server.model
{
    public class L1UbPattern
    {
        private bool _isFrozen = false;
        private Dictionary<int, List<L1UbSpawn>> _groups = new Dictionary<int, List<L1UbSpawn>>();

        public void addSpawn( int groupNumber, L1UbSpawn spawn )
        {
            if ( _isFrozen )
            {
                return;
            }

            List<L1UbSpawn> spawnList;
            _groups.TryGetValue( groupNumber, out spawnList );
            if ( spawnList == null )
            {
                spawnList = new List<L1UbSpawn>();
                _groups[groupNumber] = spawnList;
            }

            spawnList.Add( spawn );
        }

        public void freeze()
        {
            if ( _isFrozen )
            {
                return;
            }

            // 格納されているグループのスポーンリストをID順にソート
            foreach ( List<L1UbSpawn> spawnList in _groups.Values )
            {
                spawnList.Sort( 
                    delegate( L1UbSpawn x, L1UbSpawn y )
                    {
                        if ( x.getId() < y.getId() )
                        {
                            return -1;
                        }
                        if ( x.getId() > y.getId() )
                        {
                            return 1;
                        }
                        return 0;
                    } );

            }

            _isFrozen = true;
        }

        public bool isFrozen()
        {
            return _isFrozen;
        }

        public List<L1UbSpawn> getSpawnList( int groupNumber )
        {
            if ( !_isFrozen )
            {
                return null;
            }
            List<L1UbSpawn> ret;
            _groups.TryGetValue( groupNumber, out ret );
            return ret;
        }
    }
}
